package push.tools.service.framework.service;

import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import push.tools.service.framework.CryptoSupport;

import java.util.HashMap;

/**
 * 处理请求基础信息
 */
@Service
public class BaseHandleService extends CryptoSupport {
    private static final Logger logger = LoggerFactory.getLogger(BaseHandleService.class);

    // 获取请求的基本信息
    public HashMap<String, String> getRequestData(HttpServletRequest req) {
        HashMap<String, String> resMap = new HashMap<>();
        try {
            // 获取请求体
            String requestBody = req.getReader().readLine();
            if (requestBody == null || requestBody.isEmpty()) {
                resMap.put("state", "001");
                resMap.put("msg", "请求体为空");
                return resMap;
            }
            // 解析url
            String requestUrl = req.getRequestURL().toString();
            String[] arr = requestUrl.split("/");
            int index = arr.length - 1;//获取最后一个元素
            String serveName = arr[index];
            if (serveName == null || serveName.isEmpty()) {
                resMap.put("state", "002");
                resMap.put("msg", "serveName为空");
                return resMap;
            }
            //封装返回信息
            String s = decryptData(requestBody);//解密body数据
            resMap.put("state", "000");
            resMap.put("requestBody", s);
            resMap.put("serveName", serveName);
            return resMap;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
